import axios from 'axios'
import qs from 'qs'
import { message } from 'antd'

const request = axios.create({
  headers: {
    timeout: 7000,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    withCredentials: true,
    validateStatus: () => true
  }
})

// 请求头拦截器
request.interceptors.request.use((config) => {
  if (config.method === 'post') {
    config.data = qs.stringify(config.data)
  }
  return config
})

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    let message = ''
    const { status, data } = response
    if (status === 200 || status === 304) {
      return Promise.resolve(data)
    }
    if (status === 401 || status !== 403) {
      return Promise.reject(response)
    }
    return Promise.resolve(data)
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default request
